home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 44 / demos / mount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-15  |  5.7 KB  |  178 lines

  1.  
  2. /* This program is supposed to draw "Mandelbrot" shapes that resemble */
  3. /* mountains.     This is done by starting with a triangle figure and */
  4. /* successively subdividing (randomly spaced) the sides. These points */
  5. /* are then joined to form four smaller triangles within the original */
  6. /* one.  The process is repeated for each of these four triangles and */
  7. /* onto the next step of transformation.....                          */
  8.  
  9. /*      Original Pascal program written by: John O'Neill              */
  10. /*     Translated to C for the Atari ST by: Bob Ritter (Nov. 1985)    */
  11.  
  12. #include <osbind.h>
  13.  
  14. #define NUMSTEPS        7   /* too bad...any more and kabombs */
  15. #define SCALE           0.22
  16. #define PI2             6.2631853
  17.  
  18. extern float sqrt(), sin(), cos(), random();
  19.  
  20. int contrl[12], intin[128], ptsin[128], intout[128], ptsout[128];
  21. int i, handle, dummy;
  22. int xy[8], GoLeft;
  23. int numtrees;
  24. float c;
  25. char cc[5], line[80] = "Step:        Number of points:      ";
  26.  
  27. struct  tree
  28.         {
  29.         int locx;
  30.         int locy;
  31.         int left;      /* "*struct" tree would be more sophisticated, and */
  32.         int right;     /* then would use "Malloc(sizeof(struct tree))".   */
  33.         } root[5000];  /* 5K should be 'nuf  - not being a fancy man      */
  34.  
  35.  
  36. main()
  37. {
  38.    int step;
  39.    setup();
  40.    GoLeft = 1;
  41.    display(0);
  42.    for (step = 2; step <= NUMSTEPS; ++step)
  43.         {
  44.         GoLeft = 1;
  45.         c = numtrees;
  46.         transform(0);
  47.         GoLeft = 1;
  48.         v_clrwk(handle);
  49.         c = step;
  50.         ftoa(c,cc,0);
  51.         line[6] = cc[0];  /* that's a mess Bob */
  52.         c = numtrees + 1;
  53.         ftoa(c,cc,0);
  54.         line[35] = cc[4];
  55.         line[34] = cc[3];
  56.         line[33] = cc[2];
  57.         line[32] = cc[1];
  58.         line[31] = cc[0];  /* admitably this is a mess too */
  59.         v_gtext(handle,75,15,line);
  60.         display(0);
  61.         Cconin();  /*This should really be "evnt_keybd();" - but for GemBug!! */
  62.         }
  63. }
  64.  
  65. setup()
  66.         {
  67.         int mx1, my1, mx2, my2, mx3, my3;
  68.  
  69.         /* Set the system up to do GEM calls*/
  70.         appl_init();
  71.  
  72.         /* Get the handle of the desktop */
  73.         handle=graf_handle(&dummy,&dummy,&dummy,&dummy);
  74.  
  75.         /* Open the workstation. */
  76.         for (i=0; i<10; intin[i++] = 1);
  77.         intin[10] = 2;
  78.         v_opnvwk(intin, &handle, intout);
  79.  
  80.         graf_mouse(256,&dummy);
  81.         v_clrwk(handle);
  82.         graf_mouse(257,&dummy);
  83.         graf_mouse(5,&dummy);
  84.  
  85.         v_gtext(handle,15,15,"Click the mouse on the 3 starting co-ordinates.");
  86.         evnt_button(1,1,1,&mx1,&my1,&dummy,&dummy);
  87.         xy[0] = mx1;
  88.         xy[1] = my1;
  89.         xy[6] = mx1;
  90.         xy[7] = my1;
  91.         evnt_button(1,1,1,&mx2,&my2,&dummy,&dummy);
  92.         xy[2] = mx2;
  93.         xy[3] = my2;
  94.         v_pline(handle,2,xy);
  95.         evnt_button(1,1,1,&mx3,&my3,&dummy,&dummy);
  96.         xy[4] = mx3;
  97.         xy[5] = my3;
  98.         v_pline(handle,4,xy);
  99.  
  100.         graf_mouse(256,&dummy);
  101.  
  102.         numtrees = 2;  /* well, really it's one more... */
  103.         root[0].left = 1;
  104.         root[0].right = 2;
  105.         root[1].left = 0;
  106.         root[1].right = 0;
  107.         root[2].left = 0;
  108.         root[2].right = 0;
  109.         root[0].locx = mx1;
  110.         root[0].locy = my1;
  111.         root[1].locx = mx2;
  112.         root[1].locy = my2;
  113.         root[2].locx = mx3;
  114.         root[2].locy = my3;
  115.         }
  116.  
  117. midpoint(mp,x1,y1,x2,y2)
  118.         int mp, x1, x2, y1, y2;
  119.         {
  120.         float dx, dy;
  121.         double length, radius, angle;
  122.         dx = x2 - x1;
  123.         dy = y2 - y1;
  124.         length = sqrt(dx * dx + dy * dy);
  125.         radius = length * SCALE * (Random() & 100) / 100;
  126.         angle = PI2 * (Random() & 100) / 100;
  127.         root[mp].locx = (x1 + x2)/2 + cos(angle) * radius;
  128.         root[mp].locy = (y1 + y2)/2 + sin(angle) * radius;
  129.         }
  130.  
  131. transform(node)
  132.         int node;
  133.         {
  134.         if (GoLeft && (root[root[node].left].left != 0))
  135.                 transform(root[node].left);
  136.         GoLeft = 0;
  137.         if (root[root[node].right].right != 0)
  138.                 transform(root[node].right);
  139.         ftoa(c,cc,0);
  140.         cc[9] = '\0';
  141.         v_gtext(handle,30,30,cc);
  142.         --c;
  143.         midpoint(numtrees + 1,root[node].locx,root[node].locy,
  144.                 root[root[node].left].locx,root[root[node].left].locy);
  145.         midpoint(numtrees + 2,root[root[node].left].locx,
  146.                 root[root[node].left].locy,
  147.                 root[root[node].right].locx,root[root[node].right].locy);
  148.         midpoint(numtrees + 3,root[node].locx,root[node].locy,
  149.                 root[root[node].right].locx,root[root[node].right].locy);
  150.         root[numtrees + 1].left = root[node].left;
  151.         root[numtrees + 1].right = numtrees + 2;
  152.         root[numtrees + 3].left = numtrees + 2;
  153.         root[numtrees + 3].right = root[node].right;
  154.         root[numtrees + 2].left = root[root[node].left].right;
  155.         root[numtrees + 2].right = root[root[node].right].left;
  156.         root[node].left = numtrees + 1;
  157.         root[node].right = numtrees + 3;
  158.         numtrees = numtrees + 3;
  159.         }
  160.  
  161. display(node)
  162.         int node;
  163.         {
  164.         if (GoLeft && (root[root[node].left].left != 0))
  165.                 display(root[node].left);
  166.         GoLeft = 0;
  167.         if (root[root[node].right].right != 0)
  168.                 display(root[node].right);
  169.         xy[0] = root[node].locx;
  170.         xy[1] = root[node].locy;
  171.         xy[2] = root[root[node].left].locx;
  172.         xy[3] = root[root[node].left].locy;
  173.         xy[4] = root[root[node].right].locx;
  174.         xy[5] = root[root[node].right].locy;
  175.         xy[6] = xy[0];
  176.         xy[7] = xy[1];
  177.         v_pline(handle,4,xy);
  178.         }